package com.axdoc.baseinfo.dao.impl;

import java.util.List;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.stereotype.Component;

import com.axdoc.baseinfo.dao.ITitleMongoDao;
import com.axdoc.baseinfo.entity.Title;
import com.axdoc.common.core.mongo.AbstractMongoDao;
import com.axdoc.common.core.pagination.PageInfo;
import com.axdoc.common.core.pagination.PageResults;
import com.mongodb.BasicDBObject;
@Component("titleMongoDao")
public class TitleMongoDaoImpl extends AbstractMongoDao<Title> implements ITitleMongoDao{
	
	public Title getTitle(ObjectId titleId) {
		return super.get(Title.MONGO_COLLECT_NAME, titleId);
	}

	public PageResults<Title> findTitlePage(String name, PageInfo pageInfo) {
		BasicDBObject query = new BasicDBObject();
		if(!StringUtils.isEmpty(name)){
			Pattern namePattern = Pattern.compile("^.*" + name + ".*$", Pattern.CASE_INSENSITIVE);
			query.put("name", namePattern);
		}
		BasicDBObject orderBy = new BasicDBObject();
		List<Title> result = super.find(Title.MONGO_COLLECT_NAME, query, orderBy, pageInfo);
		return new PageResults<Title>(pageInfo, result);
	}

	public Title getTitleByName(String name) {
		return super.getByPropery(Title.MONGO_COLLECT_NAME, "name", name);
	}

}
